package com.stuManager.service;

import com.alibaba.fastjson.JSONObject;
import com.stuManager.dao.UserDao;
import com.stuManager.factory.DaoFactory;
import com.stuManager.model.CommonResponseModel;
import com.stuManager.model.User;

import java.sql.SQLException;
import java.util.List;

/*
    DBUserService
    处理用户接口的逻辑
    fetchAllUser  返回所有管理员用户信息
 */
public class DBUserService {
    private UserDao userDao;

    public DBUserService() {
        this.userDao = DaoFactory.getUserDao();
    }

    public CommonResponseModel fetchAllUser(String token) {
        CommonResponseModel m = new CommonResponseModel();
        if (token == null || token.length()==0) {
            //token为空，返回error1, 不进行查询和跳转等
            m.setErrorCode(1);
            m.setMessage("token为空");
        }
        else {
            try {
                User user = this.userDao.seekToken(token);
                if (user == null) {
                    m.setErrorCode(2);
                    m.setMessage("未能查询到token");
                }
                else {
                    //开始查询用户表
                    List<JSONObject> list = this.userDao.fetchUserList();
                    m.setErrorCode(0);
                    m.setMessage("查询成功");
                    m.setResult(list);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return m;
    }
}
